package ferp.core;

import java.util.Date;

/**
 * User: igorgok
 * Date: 8/15/13 9:12 AM
 */
public class Message
{
  public static final class VersionRange
  {
    public static final int NONE = 0;

    // using NONE for "from" or "to" means ignoring this parameter
    public VersionRange(int from, int to)
    {
      this.from = from;
      this.to = to;
    }

    public boolean fits(int code)
    {
      return (from == NONE || from <= code) && (to == NONE || code <= to);
    }

    private int from;
    private int to;
  }

  public static final class DateRange
  {
    public static final Date NONE = null;

    // using NONE for "from" or "to" means ignoring this parameter
    public DateRange(Date from, Date to)
    {
      this.from = from;
      this.to = to;
    }

    // 24 hours range
    public DateRange(int year, int month, int day)
    {
      this(new Date(Date.UTC(year, month, day, 0, 0, 0)),
           new Date(Date.UTC(year, month, day, 23, 59, 59)));
    }

    public boolean fits(Date date)
    {
      return (from == NONE || from.compareTo(date) <= 0) && (to == NONE || to.compareTo(date) >= 0);
    }

    private Date from;
    private Date to;
  }


  public static final String NBSP = "&nbsp;";
  //public static String im(String image, String text) {return image(image) + NBSP + text;}
  public static String image(String name) {return "<img src=\"" + name + "\"/>";}

  public VersionRange version;
  public DateRange date;
  public String text;

  public Message(VersionRange version, DateRange date, String text)
  {
    this.version = version;
    this.date = date;
    this.text = text;
  }

  public Message(String text) {this(null, null, text);}

  public boolean fits(int version, Date date)
  {
    return (this.version == null || this.version.fits(version)) && (this.date == null || this.date.fits(date));
  }
}
